<?php


class NewBugReportForm extends sfForm
{
	public static $fields = array(
	//    Name                    field          column         table
		'Summary'		=> array('summary',		'summary',		null),
		'Description'	=> array('description',	'description',	null),
		'Type'			=> array('type',		'type_id',		'BugType'),
		'Status'		=> array('status',		'status_id',	'BugStatus'),
		'Priority'		=> array('priority',	'priority_id',	'BugPriority'),
		'Milestone'		=> array('milestone',	'milestone_id',	'BugMilestone'),
		'Due date'		=> array('due_date',	'due_date',		null),
		'Creator'		=> array('creator',		'creator_id',	'User'),
		'Owner'			=> array('owner',		'owner_id',		'User'),
		'Creation time'	=> array('createdAt',	'created_at',	null),
		'Update time'	=> array('updatedAt',	'updated_at',	null)
	);
	
	public function setup()
	{
		$sortChoices = array();
		foreach(self::$fields as $key => $val)
		{
			$sortChoices[$val[1]] = $key;
		}
		
		$fieldChoices = array();
		foreach(self::$fields as $key => $val)
		{
			$fieldChoices[$val[0]] = $key;
		}
		
		$this->setWidgets(array(
			'name'				=> new sfWidgetFormInput(),
			'sort_by'			=> new sfWidgetFormChoice(array('choices' => $sortChoices)),
			'sort_direction'	=> new sfWidgetFormChoice(array('choices' => array('ASC' => 'ASC', 'DESC' => 'DESC'))),
			'global'			=> new sfWidgetFormSelectCheckbox(array('choices' => array('global' => 'Global?')))
		));
		
		$this->setValidators(array(
			'name'				=> new sfValidatorString(array('min_length' => 4, 'max_length' => 255)),
			//'sort_by'			=> new sfValidatorChoice(array('choices' => $sortChoices)),
			'sort_by'			=> new sfValidatorPass(),
			'sort_direction'	=> new sfValidatorChoice(array('choices' => array('ASC' => 'ASC', 'DESC' => 'DESC'))),
			'global'			=> new sfValidatorPass()
		));
		
		$this->setWidget('fields', new sfWidgetFormSelectCheckbox(array('choices' => $fieldChoices, 'formatter' => array($this, 'formatFields'))));
		$this->setValidator('fields', new sfValidatorPass());
		
		// summary
		$opWidget = new sfWidgetFormSelect(array('choices' => array ('like' => 'is like')), array('class' => 'criteria'));
		$opValidator = new sfValidatorChoice(array('choices' => array ('like' => 'is like')));
		$this->setWidget('conds[summary][op]', $opWidget);
		//$this->setValidator('conds[summary][op]', $opValidator);
		
		$widget = new sfWidgetFormInput(array(), array('class' => 'criteria'));
		$validator = new sfValidatorString(array('max_length' => 25, 'required' => false));
		$this->setWidget('conds[summary][val]', $widget);
		$this->setValidator('conds[summary][val]', $validator);
		
		$tf = array ('eq' => 'is', 'neq' => 'is not');
		
		foreach(self::$fields as $key => $val)
		{
			if (is_null($val[2]))
				continue;
				
			$opWidget = new sfWidgetFormSelect(array('choices' => $tf), array('class' => 'criteria'));
			$opValidator = new sfValidatorChoice(array('choices' => $tf));
			$opName = 'conds['.$val[1].'][op]';
			$this->setWidget($opName, $opWidget);
			//$this->setValidator($opName, $opValidator);
			
			$widget = new sfWidgetFormDoctrineSelect(array('model' => $val[2], 'add_empty' => 'Any'), array('class' => 'criteria'));
			$validator = new sfValidatorDoctrineChoice(array('model' => $val[2], 'required' => false));
			
			if ($val[2] == 'User')
			{
				$choices = array_merge($widget->getChoices(), array('USER' => 'Me', 'null' => 'Unset'));
				
				$widget = new sfWidgetFormSelect(array('choices' => $choices), array('class' => 'criteria'));
				$validator = new sfValidatorChoice(array('choices' => $choices, 'required' => false));
			}			
			
			$name = 'conds['.$val[1].'][val]';
			$this->setWidget($name, $widget);
			$this->setValidator($name, $validator);
		}
		
		$this->setValidator('conds', new sfValidatorPass());
		
		$this->widgetSchema->setNameFormat('bugreport[%s]');
	}
	
	public function getCondFields()
	{
		$ret = array('Summary' => 'summary');
		foreach(self::$fields as $key => $val)
		{
			if (!is_null($val[2]))
				$ret[$key] = $val[1];
		}
		
		return $ret;
	}
	
	public function formatFields($widget, $inputs)
	{
		$rows = array();
		foreach ($inputs as $input)
		{
			$rows[] = $widget->renderContentTag('div', $input['input']."\n".$input['label'], array('class' => 'colSelect'));
		}
		
		return implode("\n", $rows);
	}
	
}

?>